
//
// 110921 - AH - (Midtun) Created.
//

package org.soekrat.engine.retrieval.ptable;

import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.soekrat.engine.ontology.OntologyDatastructure;
import org.soekrat.engine.ontology.ptable.Product;
import org.soekrat.engine.ontology.ptable.ProductTable;
import org.soekrat.engine.ontology.ptable.ProductTableFactor;
import org.soekrat.engine.visitor.TokenizedLine;

/**
 * <p>
 *   Represents matches keywords did in a
 *   {@link ProductTable}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class ProductScan 
{
  private final Set<ProductTableFactor> m_rowFactorsMatched=
    new HashSet<ProductTableFactor>();
  
  private final Set<ProductTableFactor> m_colFactorsMatched=
    new HashSet<ProductTableFactor>();
  
  private Set<Product> m_productsOfMatchFactors=new HashSet<Product>();

  /**
   * <p>
   *   Constructor.
   * </p>
   * @param mine  {@link OntologyDatastructure} to match in.
   * @param input {@link TokenizedLine} to match.  
   */
  public ProductScan(ProductTable pt, TokenizedLine input)
  {
    for (String curr : input)
    {
      List<ProductTableFactor> fl=pt.getRowFactors(curr);
      for (ProductTableFactor f : fl) m_rowFactorsMatched.add(f);
      
      fl=pt.getColFactors(curr);
      for (ProductTableFactor f : fl) m_colFactorsMatched.add(f);
    }
    
    if (!m_rowFactorsMatched.isEmpty() && !m_colFactorsMatched.isEmpty())
    {
      for (ProductTableFactor row : m_rowFactorsMatched)
      {
        for (ProductTableFactor col : m_colFactorsMatched)
        {
          Product p=pt.getProduct(row, col);
          if (p!=null)
          {
            m_productsOfMatchFactors.add(p);
          }
        }
      }
    }
  }
  
  /**
   * <p>
   *   Tells if did match-
   * </p>
   * @return {@code true} if did, {@code false} if did not.
   */
  public boolean match()
  {
    return !m_productsOfMatchFactors.isEmpty();  
  }
  
  /**
   * <p>
   *   Tells if did match-
   * </p>
   * @return {@code true} if did, {@code false} if did not.
   */
  public boolean noMatch()
  {
    return m_productsOfMatchFactors.isEmpty();  
  }
  
  /**
   * <p>
   *   Gets matched
   *   {@link Factor}s.
   * </p>
   * @return {@code Factor}s.
   */
  public Set<ProductTableFactor> getMatchedFactors()
  {
    Set<ProductTableFactor> retVal=new HashSet<ProductTableFactor>();
    
    retVal.addAll(m_rowFactorsMatched);
    retVal.addAll(m_colFactorsMatched);
    
    return retVal;
  }
  
  /**
   * <p>
   *   Gets 
   *   {@link Product}s of matched
   *   {@link Factor}s.
   * </p>
   * @return {@code Product}s.
   */
  public Set<Product> getProductsOfMatchFactors()
  {
    return m_productsOfMatchFactors;
  }
  
  @Override
  public String toString() 
  {
    CharArrayWriter caw=new CharArrayWriter();
    PrintWriter pw=new PrintWriter(caw);
    
    pw.print("row-factors: ");
    pw.println(m_rowFactorsMatched);
    pw.print("col-factors: ");
    pw.println(m_colFactorsMatched);
    pw.print("products: ");
    pw.println(m_productsOfMatchFactors);
    
    pw.flush();
    return caw.toString();
  }
  
  
  public static void main(String[] args) 
  {
    String p="Makes sense";
    String f0="Arne";
    String f1="Beer";
    String f2="Laetitia";
    String f3="Wine";
    
    ProductTable t=new ProductTable();
    
    t.create(f0, f1, p);
    t.create(f2, f3, p);
    
    System.out.println("looking up by factors:");
    System.out.println("  '"+f0+"'x'"+f1+"'='"+t.getProduct(f0, f1)+"'");
    System.out.println("  '"+f1+"'x'"+f0+"'='"+t.getProduct(f1, f0)+"'");
    System.out.println("  '"+f2+"'x'"+f3+"'='"+t.getProduct(f2, f3)+"'");
    System.out.println("  '"+f0+"'x'"+f2+"'='"+t.getProduct(f0, f2)+"'");
    System.out.println("  '"+f1+"'x'"+f3+"'='"+t.getProduct(f1, f3)+"'");
    
    System.out.println();
    
    System.out.println("finding factors of product '"+p+"'");
    List<Product> products=t.getProduct(p);
    for (Product product : products) 
    {
      System.out.println("  "+product);
    }
    
    TokenizedLine input=new TokenizedLine("Makes sense", "Laetitia", "Wine", "allo");
    System.out.println("matches: "+input);
    ProductScan ps=new ProductScan(t, input);
    System.out.println(ps);
  }
  
}
